package com.ugoodtech.mdcc.client.dto;

        /* Copyright (C) Ugoodtech, Inc - All Rights Reserved
         * Unauthorized copying of this file, via any medium is strictly prohibited
         * Proprietary and confidential
         * Written by Stone Shaw, 15/11/18
         */

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ugoodtech.mdcc.core.utils.ConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResponse implements Serializable {
    private boolean success = false;
    private String error;
    private String errorDescription;
    private Long total;
    private Integer page;
    private Integer size;
    private Object data;

    public static JsonResponse successResponse() {
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setSuccess(true);
        return jsonResponse;
    }

    public static JsonResponse successResponseWithData(Object data) {
        JsonResponse jsonResponse = JsonResponse.successResponse();
        jsonResponse.setData(data);
        return jsonResponse;
    }

    public static JsonResponse successResponseWithData(Object data, Long total) {
        JsonResponse jsonResponse = JsonResponse.successResponse();
        jsonResponse.setData(data);
        jsonResponse.setTotal(total);
        return jsonResponse;
    }

    public static JsonResponse successResponseWithPageData(Page pageData) {
        JsonResponse jsonResponse = JsonResponse.successResponse();
        jsonResponse.setData(pageData.getContent());
        jsonResponse.setTotal(pageData.getTotalElements());
        jsonResponse.setPage(pageData.getNumber());
        jsonResponse.setSize(pageData.getSize());
        return jsonResponse;
    }

    public static JsonResponse successResponseWithClientDTOPageData(Page pageData) {
        return successResponseWithClientDTOPageData(pageData, null);
    }

    public static JsonResponse successResponseWithClientDTOPageData(Page pageData, Map<String, Object> source) {
        JsonResponse jsonResponse = JsonResponse.successResponse();
        List data = pageData.getContent();
        List<Object> retData = new ArrayList<>();
        for (Object obj : data) {
            retData.add(ConvertUtil.convertToClientDTO(obj, source));
        }
        jsonResponse.setData(retData);
        jsonResponse.setTotal(pageData.getTotalElements());
        return jsonResponse;
    }

    public static JsonResponse successResponseManagerWithDTOPageData(Page pageData) {
        return successResponseManagerWithDTOPageData(pageData, null);
    }

    public static JsonResponse successResponseManagerWithDTOPageData(Page pageData, Map<String, Object> source) {
        JsonResponse jsonResponse = JsonResponse.successResponse();
        List data = pageData.getContent();
        List<Object> retData = new ArrayList<>();
        for (Object obj : data) {
            retData.add(ConvertUtil.convertToManagerDTO(obj, source));
        }
        jsonResponse.setData(retData);
        jsonResponse.setTotal(pageData.getTotalElements());
        return jsonResponse;
    }

    public static JsonResponse errorResponseWithError(String error, String errorDescription) {
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setError(error);
        jsonResponse.setErrorDescription(errorDescription);
//        JSONObject object = JSONObject.fromObject(jsonResponse);
//        logger.debug("返回结果为:"+object.toString());
        return jsonResponse;
    }

    public static JsonResponse errorResponseWithError(String error, String errorDescription, Object data) {
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setError(error);
        jsonResponse.setErrorDescription(errorDescription);
        jsonResponse.setData(data);
//        JSONObject object = JSONObject.fromObject(jsonResponse);
//        logger.debug("返回结果为:"+object.toString());
        return jsonResponse;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    //    @JsonProperty("error_description")
    public String getErrorDescription() {
        return errorDescription;
    }

    public void setErrorDescription(String errorDescription) {
        this.errorDescription = errorDescription;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
//        JSONObject object = JSONObject.fromObject(this);
//        logger.debug("返回结果为:"+object.toString());
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    static Logger logger = LoggerFactory.getLogger(JsonResponse.class);
}
